package com.jiang.blackjack.card

import java.util.*
import kotlin.collections.ArrayList

/**
 *
 * @ProjectName:    BlackJack
 * @ClassName:      Dealer
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/3/1 14:32
 */
class Dealer {
    var playerCards: ArrayList<Card> = ArrayList(5)
    var dealerCards: ArrayList<Card> = ArrayList(5)
    var playerValue = 0
    private var dealerValue = 0
    var isWin = false
    var result = ""
    fun start() {
        playerCards.clear()
        dealerCards.clear()
        playerValue = 0
        dealerValue = 0
        isWin = false
        result = ""
        draw(true)
        draw(true)
        draw(false)
        draw(false)

        countTo(true)
    }

    private fun getRandomCard(): Card {
        val ran: Random = Random()
        var card: Card
        card = when (ran.nextInt(4)) {
            0 -> SpadeCard(ran.nextInt(13))
            1 -> HeartCard(ran.nextInt(13))
            2 -> ClubCard(ran.nextInt(13))
            3 -> DiamondCard(ran.nextInt(13))
            else -> SpadeCard(ran.nextInt(13))
        }
        return card
    }

    fun draw(isToPlayer: Boolean): Boolean {
        if (isToPlayer) {
            if (playerCards.size >= 5) return false else playerCards.add(getRandomCard())
        } else {
            if (dealerCards.size >= 5) return false else dealerCards.add(getRandomCard())
        }
        countTo(isToPlayer)
        return true
    }

    private fun countValue(list: ArrayList<Card>): Int {
        var valueTotal = 0
        var countToA = 0
        for (c in list) {
            if (c.value == 11) {
                countToA++
            } else {
                valueTotal += c.value
            }
        }

        valueTotal += if (countToA * 11 + valueTotal <= 21) countToA * 11 else countToA

        return valueTotal
    }

    private fun countTo(isToPlayer: Boolean) {
        if (isToPlayer) playerValue = countValue(playerCards) else dealerValue =
            countValue(dealerCards)
    }

    fun checkBurst(isToPlayer: Boolean): Boolean {
        if (isToPlayer) {
            if (playerValue > 21) return true
        } else {
            if (dealerValue > 21) return true
        }
        return false
    }

    fun thinking(): Boolean {
        if (playerValue > 21) return false
        if (dealerValue < playerValue || dealerValue < 17) {
            return draw(false)
        }
        return false
    }

    fun judge(): Boolean {
        while (thinking()) {
        }

        when {
            playerValue > 21 -> {
                isWin = false
                result = "你爆点了！"
            }
            dealerValue > 21 -> {
                isWin = true
                result = "庄家爆点了！"
            }
            playerValue > dealerValue -> {
                isWin = true
                result = "你赢了!"
            }
            else -> {
                isWin = false
                result = "你输了! "
            }
        }
        result += "\n你的点数 :$playerValue ,庄家的点数 : $dealerValue"
        return isWin
    }
}